﻿using System;
using System.Collections.Generic;
using UnityEngine;

namespace JH
{
    /// <summary>
    /// 仅在自己的子节点寻找脚本
    /// </summary>
    public static class GetComponentsInChildrenRealization
    {
        /// <summary>
        /// 仅在自己的子节点寻找脚本,或者接口
        /// </summary>
        public static List<T> GetComponentsOnlyInThisChildrenEm<T>(this GameObject go, bool includeInactive = true)
            where T : class
        {
            if (go == null)
                throw new ArgumentNullException(nameof(go), "必须传入一个游戏物体，才能获取子节点上的脚本！");

            var ret = new List<T>();
            for (int i = 0; i < go.transform.childCount; i++)
            {
                var each = go.transform.GetChild(i);
                if (!each.gameObject.activeSelf && !includeInactive) continue;
                var find = each.GetComponents<T>();
                ret.AddRange(find);
            }

            return ret;
        }
    }
}